<?php
class GlobalCache
{
       private static $_main_array_name = "_main_array_";
       private static $_local_cache = array();

       /**
        *
        * @param unknown_type $instance
        * @return Zing_Cache_ZingCache
        */
       public static function getCacheInstance($instance = '')
       {
       	   return Zing_Cache_ZingGlobalCache::getGlobalCache($instance);
       }

	   /**
        *
        * @param unknown_type $instance
        * @return Zing_Cache_ZingCache
        */
       public static function getCacheInstanceByConfig($instancename,$config)
       {
       	   return Zing_Cache_ZingGlobalCache::getCacheInstanceByConfig($instancename,$config);
       }

       public static function x_flushLocalCache($instance = '')
       {
       		Zing_Cache_ZingGlobalCache::flushLocalCache($instance);
       }

       public static function flushLocalCache($instance = '')
       {
       		GlobalCache::x_flushLocalCache($instance);
       }

       public static function x_getMultiCache($keys, $instance = '')
       {
       		$result = Zing_Cache_ZingGlobalCache::getMultiCache($keys, $instance);
       		return $result;
       }

       public static function getMultiCache($keys, $instance = '')
       {
       		return GlobalCache::x_getMultiCache($keys, $instance);
       		
       }

       //new function for cache multi-instance
       public static function x_getCache($key, $instance = '')
       {
       		$result = Zing_Cache_ZingGlobalCache::getCache($key, $instance);
       		return $result;
       }


       public static function getCache($key, $instance = '', $_skip_in_mem = 0)
       {

       		return GlobalCache::x_getCache($key, $instance);
       	 	/*
	       if(CacheProfiler::checkKeyMisses($key) && $_skip_in_mem == 0) return null;

	       if(is_array(self::$_local_cache) && isset(self::$_local_cache[$key]) && $_skip_in_mem == 0)
	       {
		       $cache = self::$_local_cache[$key];
		       if($cache != null) return $cache;
	       }

	       $cache = Globals::getCache();
	       $origin_key = $key;

	       $starttime = gettimeofday(true);
	       $result = $cache->load($key);
	       $endtime = gettimeofday(true);

	       if($result == null && $_skip_in_mem == 0)
	       {
		       CacheProfiler::addKeyMisses($origin_key);
	       }
	       CacheProfiler::pushToCacheProfiler($origin_key,$result,$starttime,$endtime);
	       if($result != null)
	       {
	       		SyncMemcache::setCache($key, $result);	//for sync memcache temporarrily
		       $result = json_decode($result, true);
	       }

	       if($_skip_in_mem == 0) self::$_local_cache[$key] = $result;

	       return $result;
			//*/

       }

       //new function for cache multi-instance
       public static function x_deleteCache($key, $instance = '')
       {
       		Zing_Cache_ZingGlobalCache::deleteCache($key, $instance);
       }

       public static function deleteCache($key, $instance = '')
       {
       	 	GlobalCache::x_deleteCache($key, $instance);
       }

       public static function flushAll()
       {
	       $cache = Globals::getCache();
	       if($cache == null) return null;
	       $cache->clean(Zend_Cache::CLEANING_MODE_ALL);
       }

       public static function x_setCache($key, $value, $instance = '', $expireTime = -1, $compress=0)
       {
       		Zing_Cache_ZingGlobalCache::setCache($key,$value,$instance,$expireTime,$compress);
       }
       public static function increment($key,  $instance = '', $value = 1)
       {
           return Zing_Cache_ZingGlobalCache::increment($key,$instance,$value);
       }
       public static function setCache($key, $value, $instance = '', $expireTime = -1, $compress=0)
       {
       		///*
       		if(is_int($instance))
       		{
       			$expireTime = $instance;
       			$_instance = '';
       			GlobalCache::x_setCache($key, $value, $_instance, $expireTime, $compress);
       		}
       		else
       		{
       			GlobalCache::x_setCache($key, $value, $instance, $expireTime, $compress);
       		}
       }

       public static function setCacheWithSubkey($key, $value, $subkey = '', $subkey1 = '')
       {
	       if($value == null) return;
	       $cache = Globals::getCache();
	       CacheProfiler::removeKeyMisses($key);

	       $value = json_encode($value);
	       $cache->save($value, $key);

	       if ($subkey != '')
	       {
		       self::addSubKey($subkey,$key);
	       }

	       if ($subkey1 != '')
	       {
		       self::addSubKey($subkey1,$key);
	       }
       }

       public static function deleteCacheBySubkey($subkey)
       {
	       $subkey_array = self::getSubKey($subkey);
	       $key 	= '';

	       if ($subkey_array != null)
	       {
		       for ($i = 0; $i < count($subkey_array); $i++)
		       {
			       $key = $subkey_array[$i];
			       if ($key != null && $key != '')
			       {
			       		self::deleteCache($key);
			       }

		       }
		       self::unsetSubkey($subkey);
	       }

       }

       static  private function unsetSubkey($subkey)
       {
           $main_array = self::getCache(self::$_main_array_name);
	       if($main_array)
	       {
		       if (array_key_exists($subkey, $main_array))
		       {
			       unset($main_array[$subkey]);
			       self::setCache(self::$_main_array_name, $main_array);
		       }

	       }
       }

       static private function getSubKey($subkey)
       {
	       if (!$main_array = self::getCache(self::$_main_array_name))
	       {
		       return null;
	       }

	       if (array_key_exists($subkey, $main_array))
	       {
		       return $main_array[$subkey];
	       }

	       return null;

       }

       static private function addSubKey($subkey, $key)
       {
	       //get main array from cache
	       if (!$main_array = self::getCache(self::$_main_array_name))
	       {
		       // main array not exits => init main array with $subkey
		       $subkey_array = array($key);
		       $main_array = array();
		       $main_array[$subkey] = $subkey_array;
		       //set new main array to cache
		       self::setCache(self::$_main_array_name,$main_array);
	       }
	       else	// main array exist in cache
	       {
		       //check element $subkey exits in array or not ?
		       if (array_key_exists($subkey,$main_array))
		       {
			       // subkey exist in main array
			       $subkey_array = $main_array[$subkey];
			       if (!in_array($key,$subkey_array))	// this key not exist in $subkey_array => insert it to subkey_array
			       {
				       $count = count($subkey_array);
				       $subkey_array[count($subkey_array)] = $key;
				       $main_array[$subkey] = $subkey_array;
				       self::setCache(self::$_main_array_name,$main_array);
				       //echo "<br>i'm here 2.1 -> subkey=$subkey<br>";
			       }
			       //echo "<br>i'm here 2 -> subkey=$subkey<br>";
		       }
		       else
		       {
			       // init subkey_array and set to main_array => save main_array to cache
				$subkey_array = array($key);
				$main_array[$subkey] = $subkey_array;
				self::setCache(self::$_main_array_name, $main_array);
				//echo "<br>i'm here 3 -> subkey=$subkey<br>";
		       }
	       }
       }
}

?>
